package com.fg.cloud.factory.controller.report;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.order.vo.AreaOrderReportVo;
import com.fg.cloud.manager.entry.order.vo.OrderDetailReportVo;
import com.fg.cloud.manager.entry.report.AmountEntity;
import com.fg.cloud.manager.service.report.OrderReportService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * Description:订单类报表Controller
 * @author around
 * @date 2017年9月7日下午5:30:15
 */
@Controller
@RequestMapping("orderReport")
public class OrderReportController extends BaseController {

	@Autowired
	private OrderReportService orderReportService;
	
	@RequestMapping("/sale")
	@ResponseBody
	public void findList(HttpServletResponse response, AmountEntity entity) {
		try {
			if (null == entity.getType()) entity.setType(1);
			if (null == entity.getIndex()) entity.setIndex(10);
			//处理以天为单位的查询范围
			if (1 == entity.getType()) {
				Calendar c = Calendar.getInstance();
				c.set(Calendar.DATE, c.get(Calendar.DATE)-30);
				entity.setTime(DateFormatUtils.format(c, "yyyy-MM-dd HH:mm:ss"));
			}
			
			entity.setFacId(getCurrentFactoryUser().getFacId());
			//特殊区域查询处理,封装区域ID，区域编码
			entity.setAreaNumber(entity.getAreaNumber() + "_" + entity.getAreaId());
			
			List<AmountEntity> list = orderReportService.findSale(entity);
			System.out.println(JSON.toJSONString(list));
			doResult(response, true, StatusCode.FINDSUCCESS, 
					list, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}
	
	/**
	 * Description:统计订单销量报表
	 * @param req
	 * @param resp
	 * @author dingjiang
	 * @date 2017年9月8日上午11:41:41
	 */
	@RequestMapping(value="/selectReport",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void selectReport( HttpServletRequest req, HttpServletResponse resp, 
			String beginTime, String endTime, Long areaId){
		try {
			Map<String, Object> condition = Maps.newHashMap();
			
			condition.put("facId", getCurrentFactoryUser().getFacId());
			condition.put("beginTime", beginTime);
			condition.put("endTime", endTime);
			condition.put("areaId", areaId );
			
			List<OrderDetailReportVo> orderReport = orderReportService.findReport(condition);
			if (null == orderReport) orderReport = Lists.newArrayList();
			doResult(resp, true, StatusCode.FINDSUCCESS, orderReport, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:统计区域订单&退货单报表
	 * @param req
	 * @param resp
	 * @param beginTime
	 * @param endTime
	 * @param areaNumberSort 区域信息ID集合
	 * @param type
	 * @author dingjiang
	 * @date 2017年9月12日下午4:21:13
	 */
	@RequestMapping(value="/areaOrderRpt",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void areaOrderRpt( HttpServletRequest req, HttpServletResponse resp, 
			String beginTime, String endTime, String areaNumberSort, Byte type){
		try {
			Map<String, Object> condition = Maps.newHashMap();
			if ( type == null ) type = 1;
			//绑定前端传参
			condition.put("facId", getCurrentFactoryUser().getFacId());
			condition.put("beginTime", beginTime);
			condition.put("endTime", endTime);

			List<AreaOrderReportVo> list = orderReportService.areaOrderReport(condition, 
					areaNumberSort, type);
			doResult(resp, true, StatusCode.FINDSUCCESS, 
					list == null ? Lists.newArrayList() : list, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_NULL);
		}
	}
	
	public static void main(String[] args) {
		Calendar c = Calendar.getInstance();
		System.out.println(DateFormatUtils.format(c, "yyyy-MM-dd HH:mm:ss"));
		c.set(Calendar.DATE, c.get(Calendar.DATE)-30);
		String str = DateFormatUtils.format(c, "yyyy-MM-dd HH:mm:ss");
		System.out.println(str);
	}
	
}
